package com.cictec.middleware.gps.aspect;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.pojo.TimingPointTM;
import com.cictec.middleware.gps.engine.TimingPoint;

@Component
@Aspect
public class TimingPointTMAspect {

	private static Logger logger = LoggerFactory.getLogger(TimingPointTMAspect.class);

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Around("execution(* com.cictec.middleware.gps.engine.service.DataStorageToRedisService.savePositionTMRedis(..))")
	public void savePositionTMRedis(JoinPoint pjp) throws Throwable {
		String devCode = (String) pjp.getArgs()[0];
		TimingPoint timingPoint = (TimingPoint) pjp.getArgs()[1];

		String date = DateUtils.formatDate(timingPoint.getTime(), "yyyyMMdd");
		String redisKey = date + "-" + devCode + "-TM";

		TimingPointTM timingPointTM = new TimingPointTM(timingPoint);
		System.out.println(JSON.toJSONString(timingPointTM));
//		System.out.println(JSON.toJSONString(timingPointTM, SerializerFeature.DisableCircularReferenceDetect));
		stringRedisTemplate.opsForZSet().add(redisKey, JSON.toJSONString(timingPointTM), timingPoint.getTime().getTime());
		// 设置过期时间，默认48小时
		int expireSecond = PropertiesUtils.getIntValue("redis.busPositionTM.expire", 60 * 60 * 24 * 2);
		stringRedisTemplate.expire(redisKey, expireSecond, TimeUnit.SECONDS);
	}

	@Around("execution(* com.cictec.middleware.gps.engine.service.DataStorageToRedisService.getPositionTMFromRedis(..))")
	public List<TimingPoint> getPositionTMFromRedis(JoinPoint pjp) throws Throwable {
		String date = (String) pjp.getArgs()[0];
		String devCode = (String) pjp.getArgs()[1];
		boolean isCalculateMilage = (boolean) pjp.getArgs()[2];
		
		String redisKey = date.replace("-", "") + "-" + devCode + "-TM";
		long endTime = DateUtils.getEndDateTime(date).getTime();
		
		List<TimingPoint> rtList = new ArrayList<TimingPoint>();
		
		Set<String> results = stringRedisTemplate.opsForZSet().rangeByScore(redisKey, 0, endTime);
		Iterator<String> it = results.iterator();
		while (it.hasNext()) {
			String str = it.next().toString();
			TimingPointTM timingPointTM = JSON.parseObject(str, TimingPointTM.class);
			TimingPoint timingPoint = timingPointTM.parseTimingPoint();
			if(isCalculateMilage){
				timingPoint.setAttribute(Constants.CALCULATE_MILEAGE, Constants.CALCULATE_MILEAGE);
			}
			rtList.add(timingPoint);
		}
		return rtList;
	}

}
